Vanishing Points and Horizon

Select two pair of parallel lines

We can do that by selecting the edges of a square
cd ~/Documents/Polimi/IACV/
I = imread('images/floor_tiles_hq.jpeg');
figure(1), imshow(I); figure(1), hold on;
[ptsx, ptsy] = getpts();
a = [ptsx(1); ptsy(1); 1];
b = [ptsx(2); ptsy(2); 1];
c = [ptsx(3); ptsy(3); 1];
d = [ptsx(4); ptsy(4); 1];
Let's now define the lines where the edges lie
lab = cross(a,b);
lbc = cross(b,c);
lcd = cross(c,d);
lad = cross(a,d);

Vanishing points

v1 = cross(lab, lcd);
v2 = cross(lbc, lad);

Horizon: line at infinity

h = cross(v1, v2);

Plot

figure(1), set(0, 'DefaultLineLineWidth', 2);
% show square
for p=[[a; b], [b; c], [c; d], [a; d]]
segment(p, 1);
end
figure(1), hold off;
% show points at infinity
figure(2), imshow(I); figure(2), hold on;
set(0, 'DefaultLineLineWidth', 2);
for vert = [a, b, c, d]
p1 = [vert; v1];
p2 = [vert; v2];
segment(p1, 2);
segment(p2, 2);
end
w1 = homogeneous_to_cartesian(v1);
w2 = homogeneous_to_cartesian(v2);
axis auto;
text(w1(1), w1(2), " v1", 'Fontsize', 20);
text(w2(1), w2(2), " v2", 'Fontsize', 20);
% show line at infinity
hc = homogeneous_to_cartesian_line(h);
x_coord = [min(w1(1), w2(1)), max(w1(1), w2(1))];
y_coord = hc(1) * x_coord + hc(2);
plot(x_coord, y_coord);
figure(2), hold off;

A plane has one horizon

Draw another pair of lines and check that they meet at the line at the same horizon.
Select the pair of lines.
figure(1), imshow(I); figure(1), hold on;
[ptsx, ptsy] = getpts();
e = [ptsx(1); ptsy(1); 1];
f = [ptsx(2); ptsy(2); 1];
g = [ptsx(3); ptsy(3); 1];
l = [ptsx(4); ptsy(4); 1];
Get the lines and find their intersection
lef = cross(e, f);
lgl = cross(g, l);
v3 = cross(lef, lgl);
Check if the point belongs to the line at infinity (it won't because of some imprecisions in the selection of the points)
h' * v3
ans = 2.2329e+18
axis auto;
x_coord = [0, 1500];
y_coord = hc(1) * x_coord + hc(2);
plot(x_coord, y_coord);
for vert = [e, f, g, l]
p1 = [vert; v3];
segment(p1, 1);
end
text(v3(1)/v3(3), v3(2)/v3(3), " v3", 'Fontsize', 20);
hold off;
function cart = homogeneous_to_cartesian(h) % for points
cart = [h(1)/h(3); h(2)/h(3)];
end
function cart = homogeneous_to_cartesian_line(h) % for lines
cart = [-h(1)/h(2); -h(3)/h(2)];
end
function segment(p, figure_number) % draws a segment. p=[q1; q2] where qi are the points in hom coord
p1 = homogeneous_to_cartesian(p(1:3));
p2 = homogeneous_to_cartesian(p(4:6));
figure(figure_number), plot([ p1(1), p2(1) ], [ p1(2), p2(2) ]);
end